<!--
 * @Author 哲
 * @Date 2022-11-27 18:29:41
 * @LastEditTime 2022-11-27 18:29:42
 * @LastEditors 哲
 * @Description 
 * @FilePath /work/project/ys/hk_install/html/admin/src/views/passage/mgoods.vue
-->
<template>
  <div class="conMain">
    <el-card class="box-card">
      <div slot="header" class="topfix">
        <el-form :inline="true" :model="params" size="small" class="goodsForm" label-width="100px">
          <el-form-item label="商品ID">
            <el-input v-model="params.s_id" clearable placeholder="请输入" />
          </el-form-item>
          <el-form-item label="模板名称">
            <el-input v-model="params.s_name" clearable placeholder="请输入" />
          </el-form-item>
          <!-- <el-form-item label="状态">
            <el-select v-model="params.s_status" clearable placeholder="请选择">
              <el-option v-for="(item,index) in statuslist" :label="item.txt" :value="item.val" />
            </el-select>
          </el-form-item> -->
          <el-form-item label=" ">
            <el-button type="primary" size="small" class="searchBtn" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          </el-form-item>
        </el-form>
        <div style="text-align: right;">
          <el-button v-has="'passage/goods/add'" type="primary" size="small" class="ml10" plain @click="add"><i class="el-icon-plus" /> 添加模板</el-button>
        </div>
      </div>
      <el-table v-loading="loading" :data="list" style="width: 100%" :empty-text="emtypetxt">
        <el-table-column prop="id" label="模板ID" align="center" min-width="100" />
        <el-table-column prop="name" label="模板名称" :show-overflow-tooltip="true" align="center" min-width="120" />
        <el-table-column prop="bg_colour" label="背景颜色" :show-overflow-tooltip="true" align="center" min-width="120" />
         <el-table-column label="页头图片" align="center" min-width="300">
            <template slot-scope="scope">
             <template v-for="img in scope.row.bg_img_head" >
                <el-image style="width:40px;height:40px;margin-right:10px" :src="img">
                    <div slot="error" class="image-slot" />
                </el-image>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="页脚图片" align="center" min-width="300">
          <template slot-scope="scope">
            <template v-for="img in scope.row.bg_img_foot">
                <el-image style="width:40px;height:40px;margin-right:10px" :src="img">
                    <div slot="error" class="image-slot" />
                </el-image>
            </template>
          </template>
        </el-table-column>
        <!-- <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 1" type="success" size="mini" disable-transitions>上线</el-tag>
            <el-tag v-if="scope.row.status === -1" type="info" size="mini" disable-transitions>下线</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column prop="createtime" label="创建时间" align="center" width="200" />
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-link v-has="'passage/goods/edit'" :underline="false" type="primary" title="编辑" @click="edit(scope.row)">编辑</el-link>
            <!-- <el-link v-has="'passage/goods/reset'" class="ml10" :underline="false" type="warning" title="佣金重置" @click="reset(scope.row)">重置</el-link>
            <el-link v-has="'passage/goods/reset'" class="ml10" :underline="false" type="danger" title="佣金同步" @click="resetOne(scope.row)">同步</el-link> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog
      :title="sub.id==0?'添加模板':'编辑模板'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      center
      width="40%"
    >
      <el-form ref="ruleForm" :model="sub" :rules="rules" label-width="110px">
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="sub.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="背景颜色" prop="bg_colour">
          <el-input v-model="sub.bg_colour" clearable placeholder="请输入RGB背景色" />
        </el-form-item>
        <el-form-item label="落地页头">
            <el-template v-for="(item, index) in sub.bg_img_head">
              <div class="upLoadPicBox" @click="modalBgImgHead(index)">
                <div v-if="item != ''" class="upictrue"><img :src="item"></div>
                <div v-else class="upLoad">
                  <i class="el-icon-camera cameraIconfont" />
                </div>
              </div>
            </el-template >
            <div class="upLoadPicBox">
             <!-- <el-button size="mini" @click="addBgImgHead()">追加</el-button> -->
             <el-button size="mini" @click="resBgImgHead()">重置</el-button>
            </div>
          </el-form-item>
          <el-form-item label="落地页脚">
            <el-template v-for="(item, index) in sub.bg_img_foot">
              <div class="upLoadPicBox" @click="modalBgImgFoot(index)">
                <div v-if="item != ''" class="upictrue"><img :src="item"></div>
                <div v-else class="upLoad">
                  <i class="el-icon-camera cameraIconfont" />
                </div>
              </div>
            </el-template >
            <div class="upLoadPicBox">
             <!-- <el-button size="mini" @click="addBgImgFoot()">追加</el-button> -->
             <el-button size="mini" @click="resBgImgFoot()">重置</el-button>
            </div>
          </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="sub.status">
            <el-radio label="1">上线</el-radio>
            <el-radio label="-1">下线</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="权重" prop="weigh">
          <el-input v-model="sub.weigh" clearable placeholder="权重数字越大越靠前" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subForm('ruleForm')">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { index, edit, add} from '@/api/mubanPage'

export default {
  data() {
    return {
      total: 0,
      loading: true,
      emtypetxt: '',
      list: [],
    //   statuslist: [{ val: '1', txt: '上线' }, { val: '-1', txt: '下线' }],
      params: {
        s_id: '',
        s_name: '',
        s_status: '',
        ss_name: 'like',
        page: 1,
        pagesize: 10
      },
      dialogVisible: false,
      sub: {},
      rules: {
        name: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        bg_colour: [
          { required: true, message: '背景颜色不能为空', trigger: 'blur' }
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.emtypetxt = '加载中...'
      this.loading = true
      index(this.params).then(res => {
        this.list = res.data.list
        this.total = res.data.total
        this.loading = false
        if (this.list.length == 0) {
          this.emtypetxt = '暂无数据'
        }
      })
    },
    handleSearch() {
      this.list = []
      this.params.page = 1
      this.getList()
    },
    // 分页
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getList()
    },
    add() {
        this.sub=  {
            name: '',
            weigh: '',
            bg_colour: '',
            bg_img_head: [''],
            bg_img_foot: [''],
        },
        this.dialogVisible = true
    },
    subInit(){

    },
    edit(row) {
      this.sub = {
        id: row.id,
        name: row.name,
        weigh: row.weigh,
        bg_colour: row.bg_colour,
        bg_img_head: row.bg_img_head,
        bg_img_foot: row.bg_img_foot,
      }
      this.addBgImgHead()
      this.addBgImgFoot()
      this.dialogVisible = true
    },
    subForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.sub.id > 0) {
            edit(this.sub).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getList()
            })
          } else {
            add(this.sub).then(res => {
              this.$message.success(res.msg)
              this.dialogVisible = false
              this.getList()
            })
          }
        } else {
          return false
        }
      })
    },
    modalPicTap(tit, num, i) {
      const _this = this
      _this.$modalUpload(function(img) {
        if (tit === '1' && !num) {
          _this.sub.image = img[0]
        }
      }, tit)
    },
    modalBgImgHead(tit, num, i) {
      let _this = this
      _this.$modalUpload(function(img) {
          _this.sub.bg_img_head[tit] = img[0]
            _this.sub.bg_img_head.push('')
          if(tit+2 != _this.sub.bg_img_head.length){
            _this.sub.bg_img_head.pop()
          }
      }, tit)
    },
    modalBgImgFoot(tit, num, i) {
      let _this = this
      _this.$modalUpload(function(img) {
          _this.sub.bg_img_foot[tit] = img[0]
            _this.sub.bg_img_foot.push('')
          if(tit+2 != _this.sub.bg_img_foot.length){
            _this.sub.bg_img_foot.pop()
          }
      }, tit)
    },
    addBgImgHead() {
      if (this.sub.bg_img_head.length > 5) {
        return this.$message.error('最多只能添加6个参数')
      }
      if(this.sub.bg_img_head[this.sub.bg_img_head.length-1] != ''){
          this.sub.bg_img_head.push('')
      }
    },
    addBgImgFoot() {
      if (this.sub.bg_img_foot.length > 5) {
        return this.$message.error('最多只能添加6个参数')
      }
      if(this.sub.bg_img_foot[this.sub.bg_img_foot.length-1] != ''){
          this.sub.bg_img_foot.push('')
      }
    },
    resBgImgHead() {
      this.sub.bg_img_head = [''];
    },
    resBgImgFoot() {
      this.sub.bg_img_foot = [''];
    },
  }
}
</script>
